#include "level2.h"

Level2::Level2(int health, int incoming_score) : Levels()
{
    player.setHealth(health);
    high_score = incoming_score;

    bgX1 = -60; // -60
    bgX2 = -130; // -110
    bgX3 = -240; // -
    bgY=0; // is this used anymore?

    max_right_movement=400;
    max_left_movement=400;

     // More test
    jump_max = 110;
    player_current_y = 340;
    jump_spd = 4;
    player_current_floor_y = 340;

    scrolling = 2;
    scrolling_right = true; // this var is in levels

    level_alive = true;
    restart_level = false;

    player_level_x = 400;
    player_screen_x = 400;

    level_end_point = 1035 * 7;
    level_start_point = 0;

    // create enemys in the level
    Enemy tmp( 1200, 360 );
    enemys.Add_enemy( tmp );

    txtOut = new MyText(25);

    high_score=0;
    a_hit=0;
    cloudspeed=0;
    volcano_frame=0;
    volcano_speed=0;

    //MOVING BLOCK
    movingright=true;
    movingblock=0;

    top1blocks      = "         *           *      *       *     *                * * * * * * * *                                 ";
    topblocks       = "       *     *     *      *       *     *              * *                                                 ";
    midblocks       = "     *           *      *       *     *      *     * *                                                     ";
    groundblocks    = "*****************   ***********  *** *****************************              **************************";

    //music->create_audio_thread( "sounds/level2/level2music.ogg", "LF" );
    load();
}

Level2::~Level2()
{
    cout << "Destroyed level 2\n";
    delete txtOut;
    //delete grass0;
    delete small_grass0;
 //   delete first_towers;
//    delete second_towers;
}

void Level2::load( )
{
    clouds1.load("grafx/level2/clouds1.png");
    clouds2.load("grafx/level2/clouds2.png");
    volcano.load("grafx/level2/volcano.png");
    background.load("grafx/level2/background.png");
    ground.load("grafx/level2/ground.png");

    // music can be found in Levels.h
    //music->create_audio_thread( "sounds/level1music.ogg", "LF" );


    small_grass0 = new Block( 0, 0, "grafx/level1/grassblocksmall.png", 'N' );
//    //grass0 =  new Block( 1380, 210, "grafx/level1/grassblock.png", 'N' );
//    first_towers = new Block( 1600, 340, "grafx/level1/groundtower.png", 'A' );
//    second_towers = new Block( 2500, 340, "grafx/level1/groundtower.png", 'D' );

}

void Level2::print_level_images( int bgY, int bgX1, int bgX2, int bgX3)
{
    for(int i=0; i<900; i+=100)
        background.draw( i, bgY+0);

    drawclouds();
    drawvolcanos();

    // all the levels ground images are going to be printed all at once
}

void Level2::calc_screen_movement()
{
     // right movement
    if( player_screen_x > max_right_movement )
    {
        if( player_level_x > level_end_point )
        {
            cout << "End of level!1!!!\n";
            level_alive = false;
            player_level_x = level_end_point;
            player_screen_x = 400;
        }
        else
        {
            player_screen_x = max_right_movement;
            bgX1=bgX1-1; // 2
            bgX2=bgX2-2; // 5
            bgX3=bgX3-5; // 15
        }
    }

    // left movement
    if( player_screen_x < max_left_movement )
    {
        if( player_level_x < level_start_point )
        {   //cout << "Start of level!!!1!\n";
            player_level_x = level_start_point;
            player_screen_x = 400;
        }
        else
        {
            player_screen_x = max_left_movement;
            bgX1=bgX1+1;
            bgX2=bgX2+2;
            bgX3=bgX3+5;
        }
    }

} // calc_screen_movement()

void Level2::detect_bullet_firing()
{

} // detect_bullet_firing()

void Level2::Check_player_jumping()
{
    if( true == player.Is_jumping() )
    {
        player_current_y -= jump_spd;

        if( player_current_y <= (player_current_floor_y - jump_max) )
        {
            jump_spd = -jump_spd;
        }

        if( player_current_y >= 340 )
        {
            player.Set_jumping( false );
            Reset_jump_spd();
            player_current_floor_y = 340;
        }

    }
    else if( false == player.Is_jumping() )
    {
        if( false == player.Is_on_block() )
        {
            if( player_current_y < 340 )
            {
                player.Set_landed( false );
                player_current_y += jump_spd;
            }

        }

        if( player_current_y >= 340 )
        {
            player.Set_landed( true );
            Reset_jump_spd();
            player_current_floor_y = 340;
        }
    }

} // Check_player_jumping()

void Level2::draw()
{
    scrolling_right = player.Is_moving_right();
    calc_screen_movement();

    print_level_images( bgY, bgX1, bgX2, bgX3 );

    // detect who is attacking
    detect_bullet_firing();

    Check_player_jumping();


    // Draw the player, enemys and bullets on screen;
    player.Take_from_health( kitty.action(player_screen_x, player_current_y, scrolling, a_hit) );
    a_hit = player.action( player_screen_x, player_current_y );
   // enemys.Draw( bgX3, bgY + 380, player_level_x, player_screen_x );
    //bullets.Draw( screen, enemys, player );

    // draws the ground objects on the level
    draw_ground_objects();

  //  scoreTitle->drawText("HEALTH: ", 25, 25);
   // scoreTitle->drawInt(player.Get_health(), 200, 25);

    scrolling =2;


} // Draw()

// THIS FUNCTION NEEDS TO SHRINK
void Level2::draw_ground_objects()
{
//Draw Ground blocks
    int space = 0;
    int size = groundblocks.size();
    for(int i=0; i<size;i++)
    {
        if(groundblocks[i] == '*')
        {
            ground.draw( space+bgX3, bgY+425);
            space=space+45;
            ground.draw( space+bgX3, bgY+425);
            space=space+45;
        }
        else
        {   space=space+90;}
    }


//      //attempt moving block
//    if(movingblock<500 && movingright)
//        movingblock+=5;
//    if(movingblock==500)
//         movingright=false;
//    if(movingblock>0 && !movingright)
//         movingblock-=5;
//    if(movingblock==0)
//        movingright=true;
//
//    int grassX =(bgX3+800+movingblock);
//    int grassY = (bgY-200);
//    small_grass0->Draw( grassX, grassY);
//    small_grass0->Detect_collision( player, enemys, this, bullets );


//Draw Top blocks
    space =0;
    size = top1blocks.size();
    for(int i=0; i<size;i++)
    {
        if(top1blocks[i] == '*')
        {
            small_grass0->Draw( space+bgX3, 150);
            small_grass0->Detect_collision( player, enemys, this, bullets );
            //small_grass0->Detect_bullet_collision( bullets );
            space=space+100;
        }
        else
        {   space=space+100;}
    }
//Draw Top1 blocks
    space =0;
    size = topblocks.size();
    for(int i=0; i<size;i++)
    {
        if(topblocks[i] == '*')
        {
            small_grass0->Draw( space+bgX3,250);
            small_grass0->Detect_collision( player, enemys, this, bullets );
            //small_grass0->Detect_bullet_collision( bullets );
            space=space+100;
        }
        else
        {   space=space+100;}
    }
//Draw Middle blocks
    space =0;
    size = midblocks.size();
    for(int i=0; i<size;i++)
    {
        if(midblocks[i] == '*')
        {
            small_grass0->Draw( space+bgX3, 350);
            small_grass0->Detect_collision( player, enemys, this, bullets );
            //small_grass0->Detect_bullet_collision( bullets );
            space=space+100;
        }
        else
        {   space=space+100;}
    }


}//end draw_ground_objects function()

void Level2::moveleft()
{
    player.Set_moving_right( false );
    if( false == scrolling_right )
    {  // move the player animations by one
        player.Move_frame();
        player_screen_x -= 5;
        player_level_x -= 5;
        scrolling = 1;
    }
}

void Level2::moveright()
{
    player.Set_moving_right( true );
    if( true == scrolling_right )
    {
        // move the player animations by one
        player.Move_frame();
        player_screen_x += 5;
        player_level_x += 5;
        scrolling = 0;
    }
}

void Level2::moveup()
{
    if( false == player.Is_jumping() )
    {
        player.Set_jumping( true );
    }
}

void Level2::attack()
{
    player.Set_attacking( true );
    music->create_audio_thread( "sounds/effects/sword.wav", "LO" );

}

void Level2::Set_player_main_floor_y( int main_floor_y )
{
    player_current_floor_y = main_floor_y;
}

void Level2::Reset_jump_spd()
{
    jump_spd = 4;
}

void Level2::drawclouds()
{
    for(int i=50; i<8000; i+=700)
        clouds2.draw( bgX1+cloudspeed+i, 20);

    for(int i=0; i<8000; i+=1000)
       clouds1.draw( bgX1 + i, 20);

    for(int i=200; i<8000; i+=500)
        clouds2.draw( bgX1+cloudspeed+i, 90);

    for(int i=500; i<8000; i+=700)
        clouds1.draw( 1.5*bgX1 + i, 100);
       //clouds1.draw( 1.5*bgX1 + i, 100);

    cloudspeed-=1;

}

void Level2::drawvolcanos()
{
    if(volcano_speed == 40)
    {    //if(volcano_frame < 230)
            volcano_frame=115;
        volcano_speed++;
    }
    else if(volcano_speed == 45)
    {    //if(volcano_frame < 230)
            volcano_frame=230;
        volcano_speed++;
    }
    else
    {
        if(volcano_speed ==50)
           volcano_speed=0;
        if(volcano_speed <40)
            volcano_frame = 0;
        volcano_speed++;
    }


    volcano.drawAnim( bgX3+900, 330,   //posistion
                           115, 95,        //size of single frame
                 volcano_frame, 0);

    volcano.drawAnim( bgX3+1900, 330,   //posistion
                           115, 95,        //size of single frame
                 volcano_frame, 0);

}

